package cn.mumzone.pos.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Map;
import java.util.Set;

import cn.mumzone.basic.dialog.AlertDialog;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;

/**
 * Created by JoeLjt on 2019/6/14.
 * Email: lijiateng1219@gmail.com
 * Description: 统一封装的弹窗基类
 */

public class PosDialog extends AlertDialog {

    private static int mConfirmType;

    public static final int CONFIRM_TYPE_NORMAL = -200;
    public static final int CONFIRM_TYPE_SINGLE_CHOICE_SHADOW = -300;
    public static final int CONFIRM_TYPE_DOUBLE_CHOICE_SHADOW = -400;
    public static final int CONFIRM_TYPE_WITH_TEXT_SHADOW = -500;
    public static final int DIALOG_BLACK_TITLE = -600;
    public static final int DIALOG_NO_TITLE = -700;

    public PosDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
    }

    public static class PosBuilder extends AlertDialog.Builder {

        private Context mContext;
        private int mBaseLayout;

        public PosBuilder(Context context) {
            super(context);
            this.mContext = context;
            mBaseLayout = R.layout.dia_base_normal_new;
            mConfirmType = CONFIRM_TYPE_NORMAL;
        }

        public PosBuilder(Context context, int confirmType) {
            super(context);

            this.mContext = context;

            mConfirmType = confirmType;
            switch (confirmType) {
                case CONFIRM_TYPE_SINGLE_CHOICE_SHADOW:
                    mBaseLayout = R.layout.dia_base_shadow_single_choice;
                    break;
                case CONFIRM_TYPE_DOUBLE_CHOICE_SHADOW:
                    mBaseLayout = R.layout.dia_base_shadow_double_choice;
                    break;
                case CONFIRM_TYPE_WITH_TEXT_SHADOW:
                    mBaseLayout = R.layout.dia_base_shadow_with_text_new;
                    break;
                case DIALOG_BLACK_TITLE:
                    mBaseLayout = R.layout.dia_base_black_title;
                    break;
                case DIALOG_NO_TITLE:
                    mBaseLayout = R.layout.dia_base_no_title;
                    break;
                default:
                    mBaseLayout = R.layout.dia_base_normal_new;
                    mConfirmType = CONFIRM_TYPE_NORMAL;
                    break;
            }
        }

        public PosBuilder setRealDialogContent(int layoutResId) {
            View baseDialogLayout = LayoutInflater.from(mContext).inflate(mBaseLayout, null);
            FrameLayout contentView = baseDialogLayout.findViewById(R.id.fl_container);

            View inflateView = LayoutInflater.from(mContext).inflate(layoutResId, null);
            contentView.addView(inflateView);

            setViewIds();

            super.setContentView(baseDialogLayout);
            super.setWidthAndHeight(438, 312);
            return this;
        }

        private void setViewIds() {
            if (mConfirmType == DIALOG_NO_TITLE) {
                super.setConfirmViewId(R.id.lb_confirm);
                return;
            }
            super.setCancelViewId(R.id.iv_dialog_close);
            if (mConfirmType == CONFIRM_TYPE_DOUBLE_CHOICE_SHADOW) {
                super.setDoubleConfirmViewId(R.id.tv_choice_left, R.id.lb_choice_right);
            } else {
                super.setConfirmViewId(R.id.lb_confirm);
                if (mConfirmType == CONFIRM_TYPE_WITH_TEXT_SHADOW) {
                    super.setBottomTextViewId(R.id.tv_bottom_text);
                }
            }

        }

        public PosBuilder setDialogTitle(String dialogTitle) {
            if (mConfirmType == DIALOG_NO_TITLE) {
                return this;
            }
            super.setText(R.id.tv_dialog_title, dialogTitle);
            return this;
        }

        public PosBuilder setSingleConfirmText(String confirmTitle) {
            View contentView = super.P.mView;
            LoadingButton lbConfirm = contentView.findViewById(R.id.lb_confirm);
            lbConfirm.setText(confirmTitle);
            return this;
        }

        public PosBuilder setDoubleChoiceText(String left, String right) {
            if (mConfirmType != CONFIRM_TYPE_DOUBLE_CHOICE_SHADOW) {
                throw new RuntimeException("Confirm type can not match!");
            }
            super.setText(R.id.tv_choice_left, left);

            View contentView = super.P.mView;
            if (contentView == null) {
                throw new RuntimeException("method setContentView() must be called! ");
            }
            LoadingButton lbRight = contentView.findViewById(R.id.lb_choice_right);
            lbRight.setText(right);
            return this;
        }

        public PosBuilder setBackground(int viewId, int drawableResId) {
            super.setTextBackground(viewId, drawableResId);
            return this;
        }

        public PosBuilder setUseRadiusBg(boolean useRadiusBg) {
            super.setTextBackground(R.id.dialog_bg, useRadiusBg ? R.drawable.shape_bg_dialog : R.color.white_color);
            return this;
        }

        public PosBuilder setBottomText(String targetText, Map<String, Integer> keywordMap) {
            View contentView = super.P.mView;
            if (contentView == null) {
                throw new RuntimeException("method setContentView() must be called! ");
            }
            if (mConfirmType != CONFIRM_TYPE_WITH_TEXT_SHADOW) {
                return this;
            }
            StringSpanUtils str = StringSpanUtils.init().addStr(targetText);
            if (keywordMap != null) {
                Set<String> keywords = keywordMap.keySet();
                for (String key : keywords) {
                    Integer keywordColor = keywordMap.get(key);
                    str.setForeColorWithKeyAndPosition(key, 0, mContext.getResources().getColor(keywordColor));
                }
            }
            super.setText(P.mBottomTextViewId, str.build());
            return this;
        }

        public PosBuilder setDialogNoConfirm() {
            View contentView = super.P.mView;
            if (contentView == null) {
                throw new RuntimeException("method setContentView() must be called! ");
            }
            if (mConfirmType == CONFIRM_TYPE_NORMAL) {
                LoadingButton lbConfirm = contentView.findViewById(R.id.lb_confirm);
                lbConfirm.setVisibility(View.GONE);
            }
            return this;
        }

        public PosBuilder setDialogUseBack(boolean useBack, View.OnClickListener onClickListener) {
            View contentView = super.P.mView;
            if (contentView == null) {
                throw new RuntimeException("method setContentView() must be called! ");
            }
            if (mConfirmType == DIALOG_NO_TITLE) {
                return this;
            }
            if (!useBack) {
                ImageView ivBack = contentView.findViewById(R.id.iv_dialog_back);
                ivBack.setVisibility(View.GONE);
                ivBack.setOnClickListener(null);
            } else {
                ImageView ivBack = contentView.findViewById(R.id.iv_dialog_back);
                ivBack.setVisibility(View.VISIBLE);
                ivBack.setOnClickListener(onClickListener);
            }
            return this;
        }

        public PosBuilder setWidth(int width) {
            super.setWidthAndHeight(width, ViewGroup.LayoutParams.WRAP_CONTENT);
            return this;
        }

    }

    public TextView getBottomTextView() {
        if (mConfirmType == CONFIRM_TYPE_WITH_TEXT_SHADOW) {
            TextView bottomTv = super.getView(R.id.tv_bottom_text);
            return bottomTv;
        }
        return null;
    }

}
